package com.chaopei.day37.controller;

import com.chaopei.day37.entity.Cool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Corday 天涯
 * @Date 2022/4/21 11:31
 */
@Controller
public class MyController {
    //映射动作,跳转到index.html
    //http://localhost:8080/
    @RequestMapping("/")
    //HttpServletRequest/Model/Map/ModelMap,前端可以直接通过请求对象
    // 名称拿到,生命周期一样,前端直接传参回后端要用request
    //后端给前端以属性的方式传值可以使用Model/Map/ModelMap,后端之间可以互传
    //HttpSession,前端要通过session.对象名获取
    public String go(Model model,HttpServletRequest request){
        //将数据携带给前端
        model.addAttribute("pic","gok");
        request.setAttribute("tips","提示你一下");
        Cool cool = new Cool(1008,"小美");
        model.addAttribute("cool",cool);
        List<String> list= new ArrayList<>();
        list.add("mary");
        list.add("tom");
        list.add("bean");
        model.addAttribute("list",list);
        model.addAttribute("x","yes");
        //逻辑视图
      return "index";
    }
    @RequestMapping("/play")
    @ResponseBody
    public List<String> play(HttpServletRequest request){
        List<String> list= new ArrayList<>();
        list.add("tom");
        list.add("bean");
        list.add("mary");
        String temp = request.getParameter("msg");
        //String temp = model.getAttribute("msg").toString();
        list.add(temp);
        //跳到另一个页面
        return list;
    }
}
